const fs = require('fs');
const path = require('path');
const http = require('http');
const url = require('url');
const common = require('./module/common.js');

http.createServer(function (req, res) {
  // 1 获取路径
  let pathname = url.parse(req.url).pathname; // url.parse处理js文件后缀哈希值
  pathname = pathname === "/" ? "/index.html" : pathname;
  // 通过fs模块获取文件
  if (pathname !== "/favicon.ico") {
    // 获取后缀名
    let extname = path.extname(pathname);
    fs.readFile(`./static${pathname}`, (err, data) => {
      if(err) {
        res.writeHead(200, {'Content-Type': `text/html;charset=utf-8`});
        res.end('页面丢失404');
        return
      }
      res.writeHead(200, {'Content-Type': `${common.setType(extname)};charset=utf-8`});
      res.end(data);
    })
  }
}).listen(3000);

console.log('Server running at http://127.0.0.1:3000/');